﻿using Potential_Software.Win32;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace Abide
{
    public partial class Splash : Form
    {
        public volatile bool Exit = false;
        const string UpdateUrl = "https://dl.dropboxusercontent.com/u/238557949/Potential%20Software/Abide/Update.xml";
        protected override System.Windows.Forms.CreateParams CreateParams
        {
            get
            {
                if (DesignMode)
                    return base.CreateParams;

                System.Windows.Forms.CreateParams Params = base.CreateParams;
                Params.ExStyle = (int)(WinUser.WS_EX_LAYERED);
                return Params;
            }
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);

            System.Drawing.Bitmap FormBitm = new System.Drawing.Bitmap(Width, Height);

            using (System.Drawing.Graphics imgGfx = System.Drawing.Graphics.FromImage(FormBitm))
            {
                using (System.Windows.Forms.PaintEventArgs pe = new System.Windows.Forms.PaintEventArgs(imgGfx, new System.Drawing.Rectangle(0, 0, Width, Height)))
                {
                    OnPaintLayeredWindow(pe);
                }
            }

            System.IntPtr screenDc = User32.GetDC(System.IntPtr.Zero);
            System.IntPtr memDc = Gdi32.CreateCompatibleDC(screenDc);
            System.IntPtr hBitmap = System.IntPtr.Zero;
            System.IntPtr oldBitmap = System.IntPtr.Zero;

            try
            {
                hBitmap = FormBitm.GetHbitmap(System.Drawing.Color.FromArgb(0x0));
                oldBitmap = Gdi32.SelectObject(memDc, hBitmap);

                SIZE size = new SIZE(); size.cx = FormBitm.Width; size.cy = FormBitm.Height;
                POINT pointSource = new POINT(); pointSource.x = 0; pointSource.y = 0;
                POINT topPos = new POINT(); topPos.x = Left; topPos.y = Top;

                BLENDFUNCTION blend = new BLENDFUNCTION();
                blend.BlendOp = WinUser.AC_SRC_OVER;
                blend.BlendFlags = 0x0;
                blend.SourceConstantAlpha = 0xFF;
                blend.AlphaFormat = WinUser.AC_SRC_ALPHA;

                User32.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, WinUser.ULW_ALPHA);
            }
            finally
            {
                User32.ReleaseDC(System.IntPtr.Zero, screenDc);

                if (hBitmap != System.IntPtr.Zero)
                {
                    Gdi32.SelectObject(memDc, oldBitmap);
                    Gdi32.DeleteObject(hBitmap);
                }

                Gdi32.DeleteDC(memDc);
                FormBitm.Dispose();
            }
        }
        protected virtual void OnPaintLayeredWindow(System.Windows.Forms.PaintEventArgs e)
        {
            //Clear Background
            e.Graphics.Clear(Color.Transparent);
            e.Graphics.DrawImage(Properties.Resources.Abide_Splash, new Rectangle(Point.Empty, Size));
        }
        public Splash()
        {
            InitializeComponent();
        }
    }
}
